package com.retail.admin.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 版本信息控制器
 *
 * @author retail
 * @since 2024-09-25
 */
@RestController
@RequestMapping("/api/version")
public class VersionController {
    
    private static final Logger log = LoggerFactory.getLogger(VersionController.class);

    @GetMapping
    public Map<String, Object> getVersion() {
        log.info("获取版本信息请求");
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "操作成功");
        result.put("success", true);
        
        Map<String, Object> versionInfo = new HashMap<>();
        versionInfo.put("applicationName", "零售中台系统");
        versionInfo.put("version", "1.0.0");
        versionInfo.put("buildDate", "2024-09-25");
        versionInfo.put("buildTime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        versionInfo.put("javaVersion", System.getProperty("java.version"));
        versionInfo.put("springBootVersion", "3.2.0");
        versionInfo.put("environment", System.getProperty("spring.profiles.active", "default"));
        versionInfo.put("serverPort", System.getProperty("server.port", "8080"));
        
        result.put("data", versionInfo);
        
        log.info("版本信息响应: {}", result);
        return result;
    }

    @GetMapping("/health")
    public Map<String, Object> getHealth() {
        log.info("获取健康检查请求");
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "服务健康");
        result.put("success", true);
        
        Map<String, Object> healthInfo = new HashMap<>();
        healthInfo.put("status", "UP");
        healthInfo.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        healthInfo.put("uptime", System.currentTimeMillis());
        healthInfo.put("memory", getMemoryInfo());
        
        result.put("data", healthInfo);
        
        log.info("健康检查响应: {}", result);
        return result;
    }
    
    private Map<String, Object> getMemoryInfo() {
        Runtime runtime = Runtime.getRuntime();
        Map<String, Object> memoryInfo = new HashMap<>();
        memoryInfo.put("totalMemory", runtime.totalMemory());
        memoryInfo.put("freeMemory", runtime.freeMemory());
        memoryInfo.put("usedMemory", runtime.totalMemory() - runtime.freeMemory());
        memoryInfo.put("maxMemory", runtime.maxMemory());
        return memoryInfo;
    }
}

